using voidbox;
using System;
using System.Numerics;
using TTEngine.Graphics;
using TTEngine.Resource;
using TTEngine.TTMath;

namespace TTEngine.Framework.UI
{
    public class UIButton : UIBaseElement
    {
        private UIBaseContainer _elemNormal;
        private Color _colorNormal = Color.White;
        private Color _colorPress = new Color(0.9f, 0.9f, 0.3f, 1f);
        private KeyCode _bindKey;
        private bool _press;
        private bool _keyPress;
        private int _pressId = -1;

        public Action OnClick { get; set; }
        public Action<int> OnPressDown { get; set; }
        public Action OnPressUp { get; set; }

        public UIButton(PackedTextureSpriteScale9 scale9,IFont font,float scale)
        {
            LocalRect.SetByRect(new Rectangle(0, 0, 100, 32));

            var normal = new UIContainer();
            normal._parent = this;
            normal.LocalRect.SetAsFill();

            var normalBack = new UIScale9(scale9);
            normalBack.scale = scale;
            normal.AddChild(normalBack);
            normalBack.LocalColor = Color.White;
            normalBack.LocalRect.SetAsFill();

            var txt = new UILabel();
            txt.LocalRect.SetAsFill();
            txt.Text = "Button";
            txt.Font = font;
            txt._fontScale = new Vector2(scale, scale);
            normal.AddChild(txt);

            _elemNormal = normal;
        }

        public void SetText(string text)
        {
            if (_elemNormal == null || _elemNormal.GetChildCount() < 2)
                return;

            var e = _elemNormal.GetChild(1);
            if (e is UILabel el)
            {
                el.Text = text;
            }

            return;
        }


        public void UsePress(int pressId)
        {
            if (_pressId == -1)
            {
                _pressId = pressId;
                _press = true;
            }
        }

        public void CancelTouch()
        {
            _press = false;
            _pressId = -1;
        }

        public override bool OnTouch(UICanvas canvas, int touchId, bool press, bool move, float x, float y)
        {
            bool kill = base.OnTouch(canvas, touchId, press, move, x, y);
            if (kill) return true;

            var rect = GetWorldRect();
            float x2 = rect.x2;
            float y2 = rect.y2;

            if (!_press && press && !move)
            {
                if (x >= rect.x1 && x < x2 && y >= rect.y1 && y < y2)
                {
                    _press = true;
                    _pressId = touchId;
                    if (OnPressDown != null)
                    {
                        canvas.InvokeEvent(() => OnPressDown(touchId));
                    }
                    return true;
                }
            }

            if (_press && press && _pressId == touchId)
            {
                return true;
            }

            if (_press && !press && _pressId == touchId)
            {
                _press = false;
                _pressId = -1;
                if (OnPressUp != null)
                {
                    canvas.InvokeEvent(() => OnPressUp());
                }
                if (x >= rect.x1 && x < x2 && y >= rect.y1 && y < y2)
                {
                    if (OnClick != null)
                    {
                        canvas.InvokeEvent(() => OnClick());
                    }
                    return true;
                }
            }
            return false;
        }

        public override void OnRender(UICanvas canvas)
        {
            if (_elemNormal != null)
            {
                _elemNormal._parent = this;
                _elemNormal.OnRender(canvas);
            }
            base.OnRender(canvas);
        }

        public override void OnUpdate(UICanvas canvas, float delta)
        {
            base.OnUpdate(canvas, delta);

            if (_bindKey != KeyCode.Unknown)
            {
                bool keyDown = TTEngine.API.view.input.IsKeyDown(_bindKey);
                if (_keyPress && !keyDown)
                {
                    _keyPress = false;
                    _press = false;
                    OnPressUp?.Invoke();
                }
                else if (!_keyPress && keyDown)
                {
                    _keyPress = true;
                    _press = true;
                    OnPressDown?.Invoke(0);
                }
            }

            if (_press)
            {
                if (_elemNormal != null)
                {
                    _elemNormal.LocalColor = _colorPress;
                    _elemNormal.OnUpdate(canvas, delta);
                }
            }
            else
            {
                if (_elemNormal != null)
                {
                    _elemNormal.LocalColor = _colorNormal;
                    _elemNormal.OnUpdate(canvas, delta);
                }
            }
        }
    }
}